<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>解析json字符串</title>
</head>
<body>
  JSON对象的parse方法用于解析一个数组或由键值对组成的对象
  <script>
    // 解析JavaScript Object Notation对象字符串
    var json = '{ "first": false, "second": 2, "3rd": "string", "4th": [ "w", "x", "y", "z" ] }';
    var obj = JSON.parse(json, function (key, value) {
      // 如果value的值是true或false，则将其替换为字符串“布尔值”
      if (typeof (value) === "boolean") {
        value = "布尔值";
      }
      return value;
    });
    var arr = document.getElementsByTagName("body");
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        var br = document.createElement("br");
        arr[0].appendChild(br);
        arr[0].append(obj[key]);
      }
    }
    // 解析JavaScript Object Notation对象数组
    json = '[ "a", true, "string", 2, -1.5 ]';
    obj = JSON.parse(json);
    for (var i = 0; i < obj.length; i++) {
      br = document.createElement("br");
      arr[0].appendChild(br);
      arr[0].append(obj[i]);
    }
  </script>
</body>
</html>